import { Header } from "@/components";
import { PageLoading } from "@ant-design/pro-components";
import { history } from "@umijs/max";
const loginPath = "/login";

// ProLayout 支持的api https://procomponents.ant.design/components/layout
export const layout: any = ({
  initialState,
}: any) => {
  return {
    layout: "mix",
    headerRender: () => <Header />,
    onPageChange: () => {
      const { location } = history;
      // 如果没有登录，重定向到 login
      const useInfoStore = window.sessionStorage.getItem("userInfo");
      const currentUser = useInfoStore || initialState?.currentUser;
      if (!currentUser && location.pathname !== loginPath) {
        history.push(loginPath);
      }
    },
    postMenuData: (menuData: any) => {
      const currentUser: any = window.sessionStorage.getItem("userInfo") || '{}';
      const currentUserObj = JSON.parse(currentUser);
      let targetMenuData:any =  [];
      if(currentUserObj?.userName === "admin"){
        return menuData;
      }else{
        targetMenuData =  menuData.filter((item: any) => {
          return !(["/userMsg","/dictionariesMsg","/backupsMsg"].includes(item?.path));
        })
      }
      return targetMenuData;
    },
    // 增加一个 loading 的状态
    childrenRender: (children: any) => {
      if (initialState?.loading) return <PageLoading />;
      return children;
    },
    ...initialState?.settings,
  };
};
